﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using WebAppMatrix.Entity.Security;
using WebAppMatrix.Website.Common;

namespace WebAppMatrix.Website.API.SystemBase
{
    public class UserController : WebApiControllerBase
    {
        [AcceptVerbs("GET", "POST")]
        public GridAjaxResult getList()
        {
            long page = Convert.ToInt64(Params["page"]);
            long rows = Convert.ToInt64(Params["rows"]);
            string keyword = Params["keyword"].ToString();

            GridAjaxResult result = new GridAjaxResult();
            WebAppMatrix.Data.MatrixData database = WebAppMatrix.Data.MatrixData.Create();
            WebAppMatrix.DAL.Security.MatrixUser user = new DAL.Security.MatrixUser(database);
            var pageEntity = user.GetPageEntity(page, rows, keyword);
            result.rows = pageEntity.Items;
            result.total = pageEntity.TotalItems;
            return result;
        }

        [AcceptVerbs("GET", "POST")]
        public void Add(MatrixUser user)
        {
            user.UserID = MatrixGuid.NewGUID;
            user.Password = Security.Security.HashPassword(user.Password);
            WebAppMatrix.Data.MatrixData database = WebAppMatrix.Data.MatrixData.Create();
            WebAppMatrix.DAL.Security.MatrixUser matrixUser = new DAL.Security.MatrixUser(database);
            matrixUser.Insert(user);
        }

        [AcceptVerbs("GET", "POST")]
        public void Delete(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                WebAppMatrix.Data.MatrixData database = WebAppMatrix.Data.MatrixData.Create();
                WebAppMatrix.DAL.Security.MatrixUser matrixUser = new DAL.Security.MatrixUser(database);
                MatrixUser user = matrixUser.GetSingle(id);
                if (user != null)
                {
                    matrixUser.Delete(user);
                }
            }
        }
    }
}